package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.ActivityItem;
import com.sc.nft.mapper.ActivityItemMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * (sc_activity_item)数据DAO
 *
 * @author Sun
 * @since 2022-07-08 13:20:35
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class ActivityItemDao extends ServiceImpl<ActivityItemMapper, ActivityItem> {

    public List<ActivityItem> getListByActivityId(Long id) {
        LambdaQueryWrapper<ActivityItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityItem::getActivityId,id);
        wrapper.eq(ActivityItem::getStatus,Boolean.TRUE);
        wrapper.gt(ActivityItem::getUsableCount,0);
        return list(wrapper);
    }

    public void subActivityItemStock(ActivityItem activityItem,int count) {
        LambdaUpdateWrapper<ActivityItem> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("sell_count=sell_count+"+count+" , usable_count=usable_count-"+count);
        wrapper.eq(ActivityItem::getId,activityItem.getId());
//        wrapper.isNull(ActivityItem::getId);
        update(wrapper);
    }
}